package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.AddressModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ivany on 2016/1/13.
 */
public class SelectAddressAdapter extends BaseAdapter {

    private Context mContext;
    private List<AddressModel> mData;

    public SelectAddressAdapter(Context context){
        mContext=context;
        mData=new ArrayList<>();
    }

    public SelectAddressAdapter(Context context,List<AddressModel> data){
        mContext=context;
        mData=data;
    }

    public void setData(List<AddressModel> data){
        mData.clear();
        mData.addAll(data);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.item_list_address,parent,false);
            holder=new ViewHolder();
            holder.mImg_select= (ImageView) convertView.findViewById(R.id.item_address_img);
            holder.mTxt_name= (TextView) convertView.findViewById(R.id.item_address_name);
            holder.mTxt_phone= (TextView) convertView.findViewById(R.id.item_address_phone);
            holder.mTxt_address= (TextView) convertView.findViewById(R.id.item_address_detail);
            holder.mTxt_default= (TextView) convertView.findViewById(R.id.item_address_default);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }

        AddressModel model=mData.get(position);
        holder.mTxt_name.setText(model.getName());
        holder.mTxt_phone.setText(model.getPhone());
        holder.mTxt_address.setText(model.getProvince()+" "+model.getCity()+" "+model.getCounty()+" "+model.getAddress());

        if(model.isDefault()){
           // holder.mImg_select.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.select_icon));
            holder.mTxt_default.setVisibility(View.VISIBLE);
        }else{
            //holder.mImg_select.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.unselect_icon));
            holder.mTxt_default.setVisibility(View.GONE);
        }

        if(model.isSelect()){
            holder.mImg_select.setImageResource(R.mipmap.select_icon);
        }else{
            holder.mImg_select.setImageResource(R.mipmap.unselect_icon);
        }

        return convertView;
    }

    class ViewHolder{
        ImageView mImg_select;
        TextView mTxt_name;
        TextView mTxt_phone;
        TextView mTxt_address;
        TextView mTxt_default;
    }
}
